module Tuscan
  module Iec60584
    module TypeN
      T90_RANGE = -270.0..1300.0
      EMF_RANGE = -3.991..47.513

      extend self

      def emfr_unbound t90
        if t90 < 0.0
          Polynomial.new(
             0.000000000000e+00,  0.261591059620e-01,  0.109574842280e-04, -0.938411115540e-07,
            -0.464120397590e-10, -0.263033577160e-11, -0.226534380030e-13, -0.760893007910e-16,
            -0.934196678350e-19
          ).solve_for t90
        else
          Polynomial.new(
             0.000000000000e+00,  0.259293946010e-01,  0.157101418800e-04,  0.438256272370e-07,
            -0.252611697940e-09,  0.643118193390e-12, -0.100634715190e-14,  0.997453389920e-18,
            -0.608632456070e-21,  0.208492293390e-24, -0.306821961510e-28
          ).solve_for t90
        end
      end

      def t90r_guess emf
        if emf < 0.0
          Polynomial.new(
             0.0000000e+00,  3.8436847e+01,  1.1010485e+00,  5.2229312e+00,  7.2060525e+00,
             5.8488586e+00,  2.7754916e+00,  7.7075166e-01,  1.1582665e-01,  7.3138868e-03
          ).solve_for emf
        elsif emf < 20.613
          Polynomial.new(
             0.00000e+00,    3.86896e+01,   -1.08267e+00,    4.70205e-02,   -2.12169e-06,
            -1.17272e-04,    5.39280e-06,   -7.98156e-08
          ).solve_for emf
        else
          Polynomial.new(
             1.972485e+01,   3.300943e+01,  -3.915159e-01,   9.855391e-03,  -1.274371e-04,
             7.767022e-07
          ).solve_for emf
        end
      end
    end
  end
end

